﻿using IntoStationSystem.Models;
using IntoStationSystem.ViewModels;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace IntoStationSystem.Views
{
    /// <summary>
    /// RFID.xaml 的交互逻辑
    /// </summary>
    public partial class RFIDView : UserControl
    {
        RfidModel vm => Locator.Instance.Rfid;
        public RFIDView()
        {
            InitializeComponent();
            this.DataContext = vm;
        }

        //DataPos pos => Locator.Instance.Service.PlcMapping.DataMapping.DataPos.Where(o => o.Desc == "Rfid").FirstOrDefault();

        private void RfidReadDown_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
           // Locator.Instance.Service.WriteBool(pos.Offset, 3, true);
        }

        private void RfidReadUp_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Task.Run(() =>
            {
                Thread.Sleep(200);
                //Locator.Instance.Service.WriteBool(pos.Offset, 3, false);
            });
        }

        private void RfidWriteDown_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            WriteData();
            //Locator.Instance.Service.WriteBool(pos.Offset, 2, true);
        }

        private void RfidWriteUp_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            Task.Run(() =>
            {
                Thread.Sleep(200);
                //Locator.Instance.Service.WriteBool(pos.Offset, 2, false);
            });
        }

        private void WriteData()
        {
            byte[] datas = new byte[16];
            datas[0] = (byte)vm.WriteMode;
            datas[1] = (byte)vm.WriteResult;
            datas[2] = (byte)vm.WriteWorkstation;
            datas[3] = (byte)vm.WriteID1;
            datas[4] = (byte)vm.WriteID2;
            datas[5] = (byte)vm.WriteID3;
            datas[6] = (byte)vm.WriteID4;
            datas[7] = (byte)vm.WriteID5;
            datas[8] = (byte)vm.WriteID6;
            //Locator.Instance.Service.WriteData(pos.Offset + 8, datas);
        }
    }
}
